package com.yichenyang.ordersystem.controller;

import com.yichenyang.ordersystem.entity.Orderdetail;
import com.yichenyang.ordersystem.entity.Product;
import com.yichenyang.ordersystem.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductService productService;

    private int pageSize = 10;
    private int pageCount = 0;
    private int pageTotal;

    @RequestMapping("/list")
    public String memberList(HttpSession session) {
        session.setAttribute("page",1);
        int productsAll = productService.queryAll();
        session.setAttribute("productNum",productsAll);
        session.setAttribute("productPages",getPages());

        List<Product> products = productService.queryAllByLimit(0, pageSize);
        session.setAttribute("products",products);
        return "pages/article/list::productList";
    }

    @PostMapping("/add")
    @ResponseBody
    public Product memberAdd(@RequestBody Product product,HttpSession session) {
        product.setAuditing("1");
        product.setReleaseTime(getTime());
        product = productService.insert(product);
        return product;
    }

    @PostMapping("/find")
    public String memberFind(@RequestBody Map<String,String> map, HttpSession session) {
        if (session.getAttribute("products")!=null) {
            List<Product> products = productService.queryByConditions(map.get("username"), map.get("start"), map.get("end"));
            session.setAttribute("products",products);
        }
        return "pages/article/list::productList";
    }

    //修改商品信息
    @PostMapping("/update/{id}")
    @ResponseBody
    public void update(@PathVariable int id,@RequestBody Product product) {
        product.setProductId(id);
        productService.update(product);
    }

    //启用商品
    @GetMapping("/invocation/{id}")
    @ResponseBody
    public void invocation(@PathVariable int id) {
        productService.updateStatus(1,id);
    }

    //停用商品
    @GetMapping("/stop/{id}")
    @ResponseBody
    public void stop(@PathVariable int id) {
        productService.updateStatus(0,id);
    }

    //删除信息
    @GetMapping("/del/{id}")
    public String del(@PathVariable int id, HttpSession session) {
        productService.deleteById(id);
        List<Product> products = productService.queryAllByLimit(pageCount, pageSize);
        session.setAttribute("products",products);
        return "pages/article/list::productList";
    }

    //换页
    @PostMapping("/page/{page}")
    public String changePage(@PathVariable int page, HttpSession session) {
        pageCount = (page-1)*10;
        List<Product> products = productService.queryAllByLimit(pageCount, pageSize);
        session.setAttribute("products",products);
        return "pages/article/list::productList";
    }

    //获取商品信息
    @GetMapping("/message/{id}")
    @ResponseBody
    public Product getMessage(@PathVariable int id) {
        Product product = productService.queryById(id);
        return product;
    }

    public List<Integer> getPages() {
        int productsAll = productService.queryAll();
        pageTotal = productsAll / pageSize + (productsAll % pageSize == 0 ? 0:1);
        List<Integer> pages = new CopyOnWriteArrayList<>();
        for (int i = 0; i < pageTotal; i++) {
            pages.add(i+1);
        }
        return pages;
    }

    //更新最新时间
    public String getTime() {
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = dateFormat.format(date);
        return now;
    }

}
